/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.ArithUtil;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cms.dao.Record_summaryDao;
import com.thinkgem.jeesite.modules.cms.dao.TaskDao;
import com.thinkgem.jeesite.modules.cms.entity.Record_summary;
import com.thinkgem.jeesite.modules.cms.entity.Task;
import com.thinkgem.jeesite.modules.cms.entity.taskData;
import com.thinkgem.jeesite.modules.cms.entity.taskDaydata;
import com.thinkgem.jeesite.modules.cms.entity.taskMonthdata;
import com.thinkgem.jeesite.modules.cms.entity.taskYeardata;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 子任务管理Service
 * 
 * @author leo
 * @version 2017-02-14
 */
@Service("taskService")
@Transactional(readOnly = true)
public class TaskService extends CrudService<TaskDao, Task> {

	@Autowired
	private TaskDao taskDao;
	@Autowired
	private Record_summaryDao summaryDao;

	@Transactional(readOnly = false)
	public Page<Task> findPage(Page<Task> page, Task task,
			boolean isDataScopeFilter) {
		return super.findPage(page, task);
	}

	@Transactional(readOnly = false)
	public void save(Task task) {
		if (StringUtils.isBlank(task.getId())) {
			if (task.getPdate() != null) {
				Calendar mCalendar = Calendar.getInstance();
				mCalendar.setTime(task.getPdate());
				task.setPdate_year(mCalendar.get(1));
				task.setPdate_month(mCalendar.get(2) + 1);
			}
			((TaskDao) this.dao).insert(task);
		} else {
			task.preUpdate();
			((TaskDao) this.dao).update(task);
		}
	}

	@Transactional(readOnly = false)
	public void delete(Task task, Boolean isRe) {
		super.delete(task);
	}

	@Transactional(readOnly = false)
	public void clear(String end) {
		this.taskDao.clear(end);
	}

	@Transactional(readOnly = false)
	public taskData getByKeyfield(String keyfield) {
		Task task = this.taskDao.getByKeyfield(keyfield);
		if (task != null) {
			taskData data = new taskData(keyfield);
			data.setCharacter01(task.getCharacter01());
			data.setCharacter02(task.getCharacter02());
			data.setCharacter03(task.getCharacter03());
			data.setCharacter04(task.getCharacter04());
			data.setCharacter05(task.getCharacter05());
			data.setCharacter06(task.getCharacter06());
			data.setCommenttext(task.getCommenttext());
			data.setDept(task.getDept());
			data.setDptask(task.getDptask());
			data.setA(task.getA());
			data.setB(task.getB());
			data.setC(task.getC());
			data.setD(task.getD());
			data.setE(task.getE());
			data.setF(task.getF());
			data.setG(task.getG());
			data.setH(task.getH());
			data.setIsok(task.getIsok());
			data.setIspartcomplete(task.getIspartcomplete());
			data.setIsqctask(task.getIsqctask());
			data.setIsstart(task.getIsstart());
			data.setIstagprocess(task.getIstagprocess());
			data.setIstaskcomplete(task.getIstaskcomplete());
			data.setJcdept(task.getJcdept());
			data.setNumber03(task.getNumber03());
			data.setNumber07(task.getNumber07());
			data.setNumber13(task.getNumber13());
			data.setOpcode(task.getOpcode());
			data.setOpdesc(task.getOpdesc());
			data.setPartnum(task.getPartnum());
			data.setProdcode(task.getProdcode());
			data.setProductid(task.getProductid());
			data.setProductname(task.getProductname());
			data.setStarhour(task.getStarhour());
			data.setTasktype(task.getTasktype());
			data.setUserid(task.getUserid());
			data.setXpartnum(task.getXpartnum());
			return data;
		}
		return null;
	}

	@Transactional(readOnly = false)
	public Task getByKeyfield2(String keyfield) {
		return this.taskDao.getByKeyfield(keyfield);
	}

	@Transactional(readOnly = false)
	public List<Task> getNoEnd() {
		return this.taskDao.getNoEnd();
	}

	@Transactional(readOnly = false)
	public taskData getByProductid(String productid) {
		Task task = this.taskDao.getByProductid(productid);
		if (task != null) {
			taskData data = new taskData("");
			data.setCharacter01(task.getCharacter01());
			data.setCharacter02(task.getCharacter02());
			data.setCharacter03(task.getCharacter03());
			data.setCharacter04(task.getCharacter04());
			data.setCharacter05(task.getCharacter05());
			data.setCharacter06(task.getCharacter06());
			data.setCommenttext(task.getCommenttext());
			data.setDept(task.getDept());
			data.setDptask(task.getDptask());
			data.setA(task.getA());
			data.setB(task.getB());
			data.setC(task.getC());
			data.setD(task.getD());
			data.setE(task.getE());
			data.setF(task.getF());
			data.setG(task.getG());
			data.setH(task.getH());
			data.setIsok(task.getIsok());
			data.setIspartcomplete(task.getIspartcomplete());
			data.setIsqctask(task.getIsqctask());
			data.setIsstart(task.getIsstart());
			data.setIstagprocess(task.getIstagprocess());
			data.setIstaskcomplete(task.getIstaskcomplete());
			data.setJcdept(task.getJcdept());
			data.setNumber03(task.getNumber03());
			data.setNumber07(task.getNumber07());
			data.setNumber13(task.getNumber13());
			data.setOpcode(task.getOpcode());
			data.setOpdesc(task.getOpdesc());
			data.setPartnum(task.getPartnum());
			data.setProdcode(task.getProdcode());
			data.setProductid(task.getProductid());
			data.setProductname(task.getProductname());
			data.setStarhour(task.getStarhour());
			data.setTasktype(task.getTasktype());
			data.setUserid(task.getUserid());
			data.setXpartnum(task.getXpartnum());
			return data;
		}
		return null;
	}

	@Transactional(readOnly = false)
	public boolean hasStart(String productid) {
		Task task = this.taskDao.getStartProductid(productid);
		return task != null;
	}

	@Transactional(readOnly = false)
	public int start(String keyfield, String userid) {
		return this.taskDao.updateIsStart(keyfield, 1, userid);
	}

	@Transactional(readOnly = false)
	public int complete(String keyfield, String userid) {
		return this.taskDao.updateIsTaskComplete(keyfield, 1, userid);
	}

	@Transactional(readOnly = false)
	public int partcomplete(String keyfield) {
		return this.taskDao.updateIsPartComplete(keyfield);
	}

	@Transactional(readOnly = false)
	public int qcComplete(String keyfield) {
		return this.taskDao.updateIsTaskQcComplete(keyfield, 1);
	}

	@Transactional(readOnly = false)
	public int back(String keyfield, String userid) {
		return this.taskDao.updateIsTaskQcComplete(keyfield, 0);
	}

	@Transactional(readOnly = false)
	public int updateProduct(String productid, int isok) {
		if (isok == 1) {
			return this.taskDao.updateOk(productid);
		}
		return this.taskDao.updateNoOk(productid);
	}

	@Transactional(readOnly = false)
	public taskYeardata getYeardata(String userid) {
		Calendar cal = Calendar.getInstance();
		taskYeardata data = new taskYeardata();
		List<Task> tasks = this.taskDao.getYeardata(userid, cal.get(1));
		int totalnum = 0;
		int num1 = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		int num5 = 0;
		int num6 = 0;
		int num7 = 0;
		int num8 = 0;
		int num9 = 0;
		int num10 = 0;
		int num11 = 0;
		int num12 = 0;
		double totalgdp = 0.0D;
		double gdp1 = 0.0D;
		double gdp2 = 0.0D;
		double gdp3 = 0.0D;
		double gdp4 = 0.0D;
		double gdp5 = 0.0D;
		double gdp6 = 0.0D;
		double gdp7 = 0.0D;
		double gdp8 = 0.0D;
		double gdp9 = 0.0D;
		double gdp10 = 0.0D;
		double gdp11 = 0.0D;
		double gdp12 = 0.0D;

		List<Record_summary> summary = this.summaryDao.getSureMonth(userid,
				cal.get(1));
		for (Task task : tasks) {
			totalnum++;
			totalgdp = ArithUtil.add(totalgdp, getgdpABC(task));
			cal.setTime(task.getPdate());
			int month = cal.get(2) + 1;
			switch (month) {
			case 1:
				num1++;
				gdp1 = ArithUtil.add(gdp1, getgdpABC(task));
				break;
			case 2:
				num2++;
				gdp2 = ArithUtil.add(gdp2, getgdpABC(task));
				break;
			case 3:
				num3++;
				gdp3 = ArithUtil.add(gdp3, getgdpABC(task));
				break;
			case 4:
				num4++;
				gdp4 = ArithUtil.add(gdp4, getgdpABC(task));
				break;
			case 5:
				num5++;
				gdp5 = ArithUtil.add(gdp5, getgdpABC(task));
				break;
			case 6:
				num6++;
				gdp6 = ArithUtil.add(gdp6, getgdpABC(task));
				break;
			case 7:
				num7++;
				gdp7 = ArithUtil.add(gdp7, getgdpABC(task));
				break;
			case 8:
				num8++;
				gdp8 = ArithUtil.add(gdp8, getgdpABC(task));
				break;
			case 9:
				num9++;
				gdp1 = ArithUtil.add(gdp1, getgdpABC(task));
				break;
			case 10:
				num10++;
				gdp10 = ArithUtil.add(gdp10, getgdpABC(task));
				break;
			case 11:
				num11++;
				gdp11 = ArithUtil.add(gdp11, getgdpABC(task));
				break;
			case 12:
				num12++;
				gdp12 = ArithUtil.add(gdp12, getgdpABC(task));
			}
		}
		data.setTotalnum(totalnum);
		data.setTotalgdp(totalgdp);
		data.setNum1(num1);
		data.setGdp1(gdp1);
		data.setNum2(num2);
		data.setGdp2(gdp2);
		data.setNum3(num3);
		data.setGdp3(gdp3);
		data.setNum4(num4);
		data.setGdp4(gdp4);
		data.setNum5(num5);
		data.setGdp5(gdp5);
		data.setNum6(num6);
		data.setGdp6(gdp6);
		data.setNum7(num7);
		data.setGdp7(gdp7);
		data.setNum8(num8);
		data.setGdp8(gdp8);
		data.setNum9(num9);
		data.setGdp9(gdp9);
		data.setNum10(num10);
		data.setGdp10(gdp10);
		data.setNum11(num11);
		data.setGdp11(gdp11);
		data.setNum12(num12);
		data.setGdp12(gdp12);
		for (Record_summary s : summary) {
			switch (s.getConfirmmonth()) {
			case 1:
				data.setSure1(1);
				break;
			case 2:
				data.setSure2(1);
				break;
			case 3:
				data.setSure3(1);
				break;
			case 4:
				data.setSure4(1);
				break;
			case 5:
				data.setSure5(1);
				break;
			case 6:
				data.setSure6(1);
				break;
			case 7:
				data.setSure7(1);
				break;
			case 8:
				data.setSure8(1);
				break;
			case 9:
				data.setSure9(1);
				break;
			case 10:
				data.setSure10(1);
				break;
			case 11:
				data.setSure11(1);
				break;
			case 12:
				data.setSure12(1);
			}
		}
		return data;
	}
	
	@Transactional(readOnly = false)
	private double getgdpABC(Task task) {
		return task.getA() + task.getB() + task.getC() + task.getD()
				+ task.getE() + task.getF() + task.getG() + task.getH();
	}

	@Transactional(readOnly = false)
	public taskMonthdata getMonthdata(String userid) {
		Calendar cal = Calendar.getInstance();
		taskMonthdata data = new taskMonthdata();
		List<taskDaydata> tasks = this.taskDao.getMonthdata(userid, cal.get(1),
				String.valueOf(cal.get(2) + 1));
		data.setDays(tasks);
		int num = 0;
		double gdp = 0.0D;
		for (taskDaydata d : tasks) {
			num += d.getTotalnum();
			gdp = ArithUtil.add(gdp, d.getTotalgdp());
		}
		data.setTotalnum(num);
		data.setTotalgdp(gdp);
		return data;
	}

	@Transactional(readOnly = false)
	public List<taskData> getDaydata(String userid) {
		Date now = new Date();
		DateFormat d2 = DateFormat.getDateInstance();
		String str2 = d2.format(now);
		List<Task> tasks = this.taskDao.getDaydata(userid, str2);
		List<taskData> datas = new ArrayList();
		if (tasks != null) {
			for (Task task : tasks) {
				taskData data = new taskData("");
				data.setA(task.getA());
				data.setB(task.getB());
				data.setC(task.getC());
				data.setD(task.getD());
				data.setE(task.getE());
				data.setF(task.getF());
				data.setG(task.getG());
				data.setH(task.getH());
				data.setOpdesc(task.getOpdesc());
				data.setKeyfield(task.getKeyfield());
				data.setPdate(d2.format(task.getPdate()));
				datas.add(data);
			}
		}
		return datas;
	}
}